package site.jlopen.service.imp;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import site.jlopen.domain.user.UserAccount;
import site.jlopen.domain.user.event.UserAccountEvent;
import site.jlopen.entity.BaseResponse;
import site.jlopen.entity.user.Datas;
import site.jlopen.entity.vo.LoginRes;
import site.jlopen.mapper.data.KusciaDataMapper;
import site.jlopen.mapper.node.KusciaNodeMapper;
import site.jlopen.mapper.node.KusciaNodeRouteMapper;
import site.jlopen.mapper.project.KusciaProjectMapper;
import site.jlopen.mapper.user.UserAccountMapper;
import site.jlopen.service.intf.user.UserService;
import site.jlopen.utils.id.IdUtils;

@Service("UserService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserAccountMapper userMapper;
	@Autowired
	private KusciaNodeMapper nodeMapper;
	@Autowired
	private KusciaDataMapper dataMapper;
	@Autowired
	private KusciaProjectMapper projectMapper;
	@Autowired
	private KusciaNodeRouteMapper nodeRouteMapper;
	
	@Override
	public BaseResponse<LoginRes> login(String username, String password) {
		UserAccount info = userMapper.selectByInfo(new UserAccount().setAccountName(username));
		if(null == info) {
			return new BaseResponse<LoginRes>().error("账号不存在");
		}
		Md5Hash md5Hash = new Md5Hash(password,"carro",1024);
		// DigestUtil.md5Hex(password)
		if(!info.getAccountPassword().equals(md5Hash.toHex())) {
			return new BaseResponse<LoginRes>().error("密码错误");
		}
		info.setAccountPassword(null);
		LoginRes res = new LoginRes(info.getAccountId(), username);
		//ThreadAuthUser.setUserId(info.getAccountId());
		return new BaseResponse<LoginRes>().success("登录成功").setData(res);
	}

	@Override
	public BaseResponse<String> register(String username, String password) {
		UserAccount info = userMapper.selectByInfo(new UserAccount().setAccountName(username));
		if(null != info) {
			return new BaseResponse<String>().error("账号不可用");
		}
		info = new UserAccount();
		info.setAccountId(IdUtils.SnowFlakeId());
		info.setAccountName(username);
		//String hashedPassword = new Sha256Hash(password, username, 1024).toHex();
		Md5Hash md5Hash = new Md5Hash(password,"carro",1024);
		info.setAccountPassword(md5Hash.toHex());
		info.setAccountRole(0);
		info.setAccountCreate(new Date());
		int row = userMapper.insert(info);
		return new BaseResponse<String>().dbRes(row, "注册成功", "注册失败");
	}

	@Override
	public BaseResponse<LoginRes> getUserByName(String username) {
		UserAccount info = userMapper.selectByName(username);
		if(null == info) {
			return new BaseResponse<LoginRes>().error("账号不存在");
		}
		LoginRes res = new LoginRes(info.getAccountId(), username);
		return new BaseResponse<LoginRes>().success("登录成功").setData(res);
	}

	@Override
	public LoginRes getLoginByName(String username) {
		UserAccount info = userMapper.selectByName(username);
		if(null == info) {
			return null;
		}
		LoginRes res = new LoginRes(info.getAccountId(), username);
		res.setPassword(info.getAccountPassword());
		return res;
	}

	@Override
	public List<UserAccountEvent> getUserList() {
		return userMapper.selectAllNotPage(null);
	}

	@Override
	public BaseResponse<String> rePsw(Long userId) {
		UserAccount info = new UserAccount();
		info.setAccountId(userId);
		Md5Hash md5Hash = new Md5Hash("123456","carro",1024);
		info.setAccountPassword(md5Hash.toHex());
		int row = userMapper.updateSelective(info);
		return new BaseResponse<String>().dbRes(row, "密码已重置【123456】");
	}

	@Override
	public BaseResponse<Datas> datas(Long userId) {
		Datas info = new Datas();
		Map<String, Object> nrecords = new HashMap<String, Object>();
		nrecords.put("nodeUserId", userId);
		info.setNodes(nodeMapper.selectAllCount(nrecords));
		Map<String, Object> drecords = new HashMap<String, Object>();
		drecords.put("dataUserId", userId);
		info.setDatas(dataMapper.selectAllCount(drecords));
		Map<String, Object> precords = new HashMap<String, Object>();
		precords.put("projectUserId", userId);
		info.setProjects(projectMapper.selectAllCount(precords));
		Map<String, Object> rrecords = new HashMap<String, Object>();
		rrecords.put("routeUserId", userId);
		info.setRoutes(nodeRouteMapper.selectAllCount(rrecords));
		return new BaseResponse<Datas>().success("OK", info);
	}

}
